package com.waiter.agent;

import java.text.DecimalFormat;
import java.util.ArrayList;

import manager.AppManager;
import models.DTO_MonAn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;

import com.tutorial.expandListView.ExpandListChild;
import com.tutorial.expandListView.ExpandListGroup;
import com.tutorial.expandListView.Adapter.ExpandListAdapter;

public class ChooseFoodActivity extends Activity {
	/** Called when the activity is first created. */
	private ExpandListAdapter ExpAdapter; // mot adapter thuc hien
	private ArrayList<ExpandListGroup> ExpListItems; // nhom cac group cho
														// expand
	private ExpandableListView ExpandList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choosefood);

		Button btnOk = (Button) findViewById(R.id.btnOK);
		Button btnCancel = (Button) findViewById(R.id.btnCancel);

		btnOk.setOnClickListener(ButtonListener());
		btnCancel.setOnClickListener(ButtonListener());
		
		
		ExpandList = (ExpandableListView) findViewById(R.id.ExpList);
		ExpListItems = SetStandardGroups();
		ExpAdapter = new ExpandListAdapter(ChooseFoodActivity.this,
				ExpListItems);
		ExpandList.setAdapter(ExpAdapter);
	}

	public OnClickListener ButtonListener() {
		return new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {
				case R.id.btnOK: {
					if (AppManager.getInstance().getState() == AppManager.ADD_FOOD_EXISTING_BILL) {
						// Add food to existing bill
						DemoAddFood();
						ChooseFoodActivity.this.finish();
						AppManager.getInstance()
								.setState(AppManager.NULL_STATE);
					} else {
						// ... Or create new bill
						DemoAddFood();
						Intent intent = new Intent(getApplicationContext(),
								OrderBillActivity.class);
						startActivity(intent);
					}
					break;
				}

				case R.id.btnCancel: {
					ChooseFoodActivity.this.finish();
					break;
				}
				
				}
			}
		};
	}

	/**
	 * Demo Add 3 food to list
	 */
	private void DemoAddFood() {
		DTO_MonAn food = new DTO_MonAn();
		food.setTenMon("Hamburger");
		food.setGia(5000);
		AppManager.getInstance().addFood(food);

		food = new DTO_MonAn();
		food.setTenMon("Hotdog");
		food.setGia(6000);
		AppManager.getInstance().addFood(food);

		food = new DTO_MonAn();
		food.setTenMon("Pizza");
		food.setGia(10000);
		AppManager.getInstance().addFood(food);
	}

	public ArrayList<ExpandListGroup> SetStandardGroups() {

		ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
		ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();

		ExpandListGroup gru1 = new ExpandListGroup();
		gru1.setName("Food");

		ExpandListChild ch1_1 = new ExpandListChild();
		ch1_1.setName("Hamburger");
		ch1_1.setTag(null);
		list2.add(ch1_1);

		ExpandListChild ch1_2 = new ExpandListChild();
		ch1_2.setName("Hotdog");
		ch1_2.setTag(null);
		list2.add(ch1_2);

		ExpandListChild ch1_3 = new ExpandListChild();
		ch1_3.setName("Pizza");
		ch1_3.setTag(null);
		list2.add(ch1_3);

		ExpandListChild ch1_4 = new ExpandListChild();
		ch1_4.setName("Soup");
		ch1_4.setTag(null);
		list2.add(ch1_4);

		gru1.setItems(list2);

		list2 = new ArrayList<ExpandListChild>();
		ExpandListGroup gru2 = new ExpandListGroup();
		gru2.setName("Water");

		ExpandListChild ch2_1 = new ExpandListChild();
		ch2_1.setName("Ice-cream");
		ch2_1.setTag(null);
		list2.add(ch2_1);

		ExpandListChild ch2_2 = new ExpandListChild();
		ch2_2.setName("Pepsi");
		ch2_2.setTag(null);
		list2.add(ch2_2);

		ExpandListChild ch2_3 = new ExpandListChild();
		ch2_3.setName("Beer");
		ch2_3.setTag(null);
		list2.add(ch2_3);

		gru2.setItems(list2);
		list.add(gru1);
		list.add(gru2);

		return list;
	}
}